package top.melody.ecommerce.platform.handler.handler;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import top.melody.ecommerce.platform.handler.enums.SecurityRole;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

/**
 * @author: blentle
 * @group: rd
 * @createdate: 2016/10/31 15:23
 * @mail: blentle.huan.ren@gmail.com
 * @description:
 * @since:1.0
 */
public class SecurityRoleHandler implements TypeHandler<SecurityRole> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, SecurityRole securityRole, JdbcType jdbcType) throws SQLException {
        if(securityRole == null){
            preparedStatement.setNull(i, Types.VARCHAR);
            return;
        }
        preparedStatement.setString(i, securityRole.getCode());
    }

    @Override
    public SecurityRole getResult(ResultSet resultSet, String s) throws SQLException {
        String role = resultSet.getString(s);
        return SecurityRole.getByCode(role);
    }

    @Override
    public SecurityRole getResult(ResultSet resultSet, int i) throws SQLException {
        String role = resultSet.getString(i);
        return SecurityRole.getByCode(role);
    }

    @Override
    public SecurityRole getResult(CallableStatement callableStatement, int i) throws SQLException {
        String role = callableStatement.getString(i);
        return SecurityRole.getByCode(role);
    }
}
